﻿using System;
using AppQlyNhaSach.Configuaration;
using AppQlyNhaSach.KhachHang;
using AppQlyNhaSach.MatHang;
using AppQlyNhaSach.NhanVien;
using AppQlyNhaSach.Sach;
using AppQlyNhaSach.TheLoai;
using DevExpress.XtraBars;
using AppQlyNhaSach.PhieuNhap;
using DAL_NhaSach;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraTabbedMdi;
using AppQlyNhaSach.HoaDon;
using AppQlyNhaSach.LichSu;
using System.Data.EntityClient;

namespace AppQlyNhaSach
{
    public partial class frmMainNS : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public static DAL_NhaSach.NhanVien nvLogIn;
        public static Boolean isTheLoai;
        public static Boolean isSach;
        public static Boolean isNCC;
        public static Boolean isPhieuNhap;
        public static Boolean isCTPhieuNhap;
        public static Boolean isKhachHang;
        public static Boolean isChucVu;
        public static Boolean isNhanVien;
        public static Boolean isLSKho;
        public static Boolean isLSNo;
        public static Boolean isHoaDon;
        public static Boolean isCTHoaDon;
        public static Boolean isThongKe;
        public static Boolean isPhieuThu;
        public frmMainNS()
        {
            InitializeComponent();
        }

        private void btnTheLoai_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (isTheLoai)
                return;
            frmTheLoai tl = new frmTheLoai();
            isTheLoai = true;
            tl.MdiParent = this;
            tl.Show();
        }

        private void btnConfig_ItemClick(object sender, ItemClickEventArgs e)
        {
            new frmSettings().ShowDialog();
        }

        private void btnSach_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (isSach)
                return;
            frmSach sach = new frmSach();
            sach.MdiParent = this;
            isSach = true;
            sach.Show();
        }

        private bool TestConnection(string cnStr)
        {
            try
            {
                // new SqlConnection(cnStr).Open();
                EntityConnection cn = new EntityConnection(cnStr);
                cn.Open();
                // Console.WriteLine("Just testing the connection.");
                cn.Close();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        private void frmMainNS_Load(object sender, EventArgs e)
        {
            // TODO: Login Check
            // DONE: Config Check
            string cnStr = Properties.Settings.Default.ConnectionStringUser;
            if (String.IsNullOrEmpty(cnStr) || cnStr.Trim().Length < 0 || !TestConnection(cnStr))
            {
                frmConfig config = new frmConfig();
                if (config.ShowDialog() != System.Windows.Forms.DialogResult.OK) this.Close();
            }
            this.MinimumSize = this.Size;
            this.MaximumSize = this.Size;
        }

        private void btnChucVu_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (isChucVu)
                return;
            frmChucVu chucvu = new frmChucVu();
            chucvu.MdiParent = this;
            isChucVu = true;
            chucvu.Show();
        }

        private void btnNhanVien_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (isNhanVien)
                return;
            frmNhanVien nv = new frmNhanVien();
            nv.MdiParent = this;
            isNhanVien = true;
            nv.Show();
        }

        private void btnKhachHang_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (isKhachHang)
                return;
            frmKhachHang kh = new frmKhachHang();
            kh.MdiParent = this;
            isKhachHang = true;
            kh.Show();
        }

        private void btnNCC_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (isNCC)
                return;
            frmNhaCungCap ncc = new frmNhaCungCap();
            ncc.MdiParent = this;
            isNCC = true;
            ncc.Show();
        }

        private void btnPhieuNhap_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (isPhieuNhap)
                return;
            frmPhieuNhap pn = new frmPhieuNhap();
            pn.MdiParent = this;
            isPhieuNhap = true;
            pn.Show();
        }

        private void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.Close();
        }

        private void btnLogInn_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmLogIn login = new frmLogIn();
            System.Windows.Forms.DialogResult r = login.ShowDialog();
            if (r == System.Windows.Forms.DialogResult.Yes)
            {
                setPermission(nvLogIn);
                btnLogOut.Enabled = true;
                txtMaNV.Caption = "Mã Nhân Viên: " + nvLogIn.MaNV.ToString();
                txtTenNV.Caption = "Tên Nhân Viên: " + nvLogIn.HoTen;
                txtCV.Caption = "Chức Vụ: " + nvLogIn.ChucVu.TenChucVu;
                btnLogInn.Enabled = false;
            }
        }
        private void setPermission(DAL_NhaSach.NhanVien nv)
        {
            if (nv.ChucVu.QLHeThong)
            {
                btnChucVu.Enabled = true;
            }
            if (nv.ChucVu.QLKhoSach)
            {
                btnSach.Enabled = true;
                btnTheLoai.Enabled = true;
                btnNCC.Enabled = true;
            }
            if (nv.ChucVu.QLNhanVien)
            {
                btnNhanVien.Enabled = true;
            }
            if (nv.ChucVu.QLNhapXuat)
            {
                btnPhieuNhap.Enabled = true;
                btnHoaDon.Enabled = true;
                btnChiTietNhap.Enabled = true;
                btnChiTietHoaDon.Enabled = true;
                btnKhachHang.Enabled = true;
            }
            if (nv.ChucVu.QLThuChi)
            {
                btnLichSuKho.Enabled = true;
                btnLichSuNo.Enabled = true;
                btnKhachHang.Enabled = true;
                btnPhieuThu.Enabled = true;
            }
        }

        private void btnLogOut_ItemClick(object sender, ItemClickEventArgs e)
        {
            System.Windows.Forms.DialogResult r = XtraMessageBox.Show("Bạn có chắc chắn muốn thoát?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
            if (r == System.Windows.Forms.DialogResult.Yes)
            {
                resetPermission();
                btnLogIn.Enabled = true;
                btnLogOut.Enabled = false;
                Application.Restart();
            }
        }
        private void resetPermission()
        {
            btnChucVu.Enabled = !true;
            btnLichSuKho.Enabled = !true;
            btnLichSuNo.Enabled = !true;
            btnPhieuNhap.Enabled = !true;
            btnHoaDon.Enabled = !true;
            btnChiTietNhap.Enabled = !true;
            btnChiTietHoaDon.Enabled = !true;
            btnNhanVien.Enabled = !true;
            btnSach.Enabled = !true;
            btnTheLoai.Enabled = !true;
            btnNCC.Enabled = !true;
            btnKhachHang.Enabled = !true;
        }

        private void btnChiTietNhap_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (isCTPhieuNhap)
                return;
            frmCTPhieuNhap pn = new frmCTPhieuNhap();
            pn.MdiParent = this;
            isCTPhieuNhap = true;
            pn.Show();
        }

        private void btnHoaDon_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (isHoaDon)
                return;
            frmHoaDon hd = new frmHoaDon();
            hd.MdiParent = this;
            isHoaDon = true;
            hd.Show();
        }

        private void btnChiTietHoaDon_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (isCTHoaDon)
                return;
            frmCTHoaDon ct = new frmCTHoaDon();
            ct.MdiParent = this;
            isCTHoaDon = true;
            ct.Show();
        }

        private void btnLichSuNo_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (isLSNo)
                return;
            frmLichSuNo ls = new frmLichSuNo();
            ls.MdiParent = this;
            isLSNo = true;
            ls.Show();
        }

        private void btnPhieuThu_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (isPhieuThu)
                return;
            frmPhieuThu pt = new frmPhieuThu();
            pt.MdiParent = this;
            isPhieuThu = true;
            pt.Show();
        }

        private void btnLichSuKho_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (isLSKho)
                return;
            frmLichSuKho ls = new frmLichSuKho();
            ls.MdiParent = this;
            isLSKho = true;
            ls.Show();
        }

    }
}